﻿using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using com.IronOne.BoardPACWinApp.Common;
using com.IronOne.BoardPACWinAppBO.Agenda;
using com.IronOne.BoardPACWinAppService.Agenda;
using com.IronOne.BoardPACWinAppUtil.Enums;
using com.IronOne.BoardPACWinAppUtil.Util;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace com.IronOne.BoardPACWinApp.View.UserControl
{
    public sealed partial class PaperInfoUserControl
    {
        public delegate void ClosePaperInfoDel(string comment, Object sender);

        private readonly IAgendaService _agendaService;

        public PaperInfoUserControl()
        {
            InitializeComponent();
            _agendaService = new AgendaService();
        }

        public static event ClosePaperInfoDel ClosePaperInfoEvt;

        private void Done_Click(object sender, RoutedEventArgs e)
        {
            if (ClosePaperInfoEvt != null) ClosePaperInfoEvt("", sender);
            ClosePopup();
        }

        /*private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            ClosePaperInfoEvt("", sender);
            ClosePopup();
        }*/

        internal async void ShowPopup(AgendaItem globalSelectdAgendaItem)
        {
            try
            {
                MRect1.Height = Window.Current.Bounds.Height;
                MRect1.Width = Window.Current.Bounds.Width;

                PopStk.Width = Window.Current.Bounds.Width / 1.5;
                PopStk.Height = Window.Current.Bounds.Height / 1.3;

                TxbComment1.Height = PopStk.Height * 0.1;
                TxbComment2.Height = PopStk.Height * 0.1;

                MembersPaperApprovalListView.Height = PopStk.Height * 0.3;

                ContentPopup.IsOpen = true;
                ContentPopup.IsLightDismissEnabled = false;

                await LoadPopupData(globalSelectdAgendaItem);
            }
            catch (Exception)
            {
            }
            finally
            {
                PgbPage.Visibility = Visibility.Collapsed;
            }
        }

        private async Task LoadPopupData(AgendaItem globalSelectdAgendaItem)
        {
            try
            {
                var last = Utility.GetDate(DateTime.Now.AddMinutes(-1)).ToString(Utility.GetDateTimeFormat());
                LastTxt.Text = String.Format(LastTxt.Text, last);

                TxbComment1.Text = globalSelectdAgendaItem.Name;

                TxbComment2.Text = String.IsNullOrEmpty(globalSelectdAgendaItem.PaperDescription) ? "Paper description not available" : globalSelectdAgendaItem.PaperDescription;

                var members =
                    await _agendaService.GetParticipantsListForMeetingWithApprovalStatus(globalSelectdAgendaItem.Id);
                if (members == null || members.Count <= 0)
                {
                    MembersPaperApprovalListViewErrTxt.Visibility = Visibility.Visible;
                    MembersPaperApprovalListView.Visibility = Visibility.Collapsed;

                    if (globalSelectdAgendaItem.DocType == (int)DocumentType.InformationPaper)
                        MembersPaperApprovalListViewErrTxt.Text =
                            "Member details not available due to approval details are not relevant to information papers";

                    return;
                }

                foreach (var item in members)
                {
                    item.ProfilePicPath = await BasePage.UpdateProfilePic(item.MemberId);
                }

                MembersPaperApprovalListView.DataContext = members;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void ClosePopup()
        {
            ContentPopup.IsOpen = false;
        }

        //Not used at the moment. Comment on the Ui page
        /*private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
            }
            catch (Exception)
            {
            }
        }*/

        /*protected async void HandleMessages(NotificationMessage<MessageDataModel> mdm)
        {
            try
            {
                await BasePage.GetInstance().HandleMessages(mdm);
                //TODO: Call LoadPopupData(); 
            }
            catch (Exception)
            {
            }
        }*/
    }
}